<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('m.system.datasource.entity.datasourceKey')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'datasourceKey', validatorRules.datasourceKey]" placeholder=""></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.datasource.entity.datasourceName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'datasourceName', validatorRules.datasourceName]" placeholder=""></a-input>
        </a-form-item>
<!--        <a-form-item v-show="isEdit" label="数据源类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'datasourceType', validatorRules.datasourceType]" placeholder="请输入类型" readOnly></a-input>
        </a-form-item>
        <a-form-item v-show="isEdit" label="数据库类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'dbType', validatorRules.dbType]" placeholder="请输入类型" readOnly></a-input>
        </a-form-item>
        <a-form-item v-show="isEdit" label="连接池类名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'classPath', validatorRules.classPath]" placeholder="请输入类路径" readOnly></a-input>
        </a-form-item>
        <a-form-item v-show="isEdit" label="是否支持聚合" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'isAggregate', validatorRules.isAggregate]" placeholder="请输入是否支持聚合" readOnly></a-input>
        </a-form-item>-->
        <a-form-item :label="$t('m.system.datasource.entity.attributesJson')" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-decorator="[ 'attributesJson', validatorRules.attributesJson]" placeholder=""></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.datasource.entity.url')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'url', validatorRules.url]" placeholder=""></a-input>
          <label>example：jdbc:mysql://127.0.0.1:3306/yuncheng?characterEncoding=UTF-8&useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai</label>
        </a-form-item>
        <a-form-item :label="$t('m.system.datasource.entity.username')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'username', validatorRules.username]" placeholder=""></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.datasource.entity.password')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'password', validatorRules.password]" placeholder="" type="password"></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.datasource.entity.driverClassName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'driverClassName', validatorRules.driverClassName]" placeholder=""></a-input>
          <label>example：com.mysql.cj.jdbc.Driver</label>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: "SysDatasourceConfigModal",
    components: {
    },
    data () {
      return {
        // isEdit:true,//修改操作
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        datasourceKey:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
        datasourceName:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
        datasourceType:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }],'initialValue':'jdbc'},
        isAggregate:{'initialValue':'true'},
        url:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
        username:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
        password:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
        driverClassName:{rules: [{ required: true, message: this.$t('m.common.tip.verify_required') }]},
         /* dbType:{rules: [{ required: true, message: '请输入数据库类型!' }]},*/
        classPath:{'initialValue':'com.alibaba.druid.pool.DruidDataSource'},
        attributesJson:{},
        },
        url: {
          add: YchConstant.PREFIX_SYSTEM + "/sysDatasourceConfig/add",
          edit: YchConstant.PREFIX_SYSTEM + "/sysDatasourceConfig/edit",
        }

      }
    },
    methods: {
      add () {
        // this.isEdit = false
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model,'datasourceKey','datasourceName','dbType','datasourceType','classPath','attributesJson','isAggregate','url','username','password','driverClassName'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            let url = formData.url;
            if(url && url.indexOf('://')>-1){
              let dbType = url.substring(url.indexOf(":")+1,url.indexOf('://'));
              formData.dbType = dbType
            }
            formData.datasourceType = 'jdbc'
            //console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.messageUtil.success(res.message);
                that.$emit('ok');
              }else{
                that.messageUtil.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(this.lodash.pick(row,'datasourceKey','datasourceName','dbType','datasourceType','classPath','attributesJson','isAggregate'))
      },

    }
  }
</script>
